/**
 * Created by John on 2016/5/19.
 */

var demo = new sample();

// demo.process();

// demo.stdout();

// demo.nextTick();

// demo.log();

// demo.trace();

// demo.inherites();

// demo.inspect();

// demo.emit();

// demo.error();

// demo.readFile();

// demo.readFile2();

// demo.read();

// demo.server();

// demo.get();

// demo.post();

// demo.http();

// demo.http2();

demo.clientRequest();

function sample() {
    this.name = "第4章";

    this.process = function () {
        console.log(process.argv);

    };

    this.stdout = function () {
        process.stdin.resume();
        process.stdin.on("data", function (data) {
            process.stdout.write("read from console: " + data.toString());
        })
    };

    this.nextTick = function () {
        function doSomething(args, callback) {
            somethingComplicated(args);
            process.nextTick(callback);
        }

        function compute() {
            var value = 999999999;
            while (value != 0) {
                value--;
            }
        }

        function somethingComplicated(args) {
            console.log(args);
        }

        doSomething(0, compute);
    };

    this.log = function () {
        console.log("Hello world");
        console.log("byvoid%diovyb");
        console.log("byvoid%ddiovyb", 1991);
    };

    this.trace = function () {
        console.trace();
    };

    this.inherites = function () {
        var util = require("util");

        function base() {
            this.name = "base";
            this.base = 1991;

            this.sayHello = function () {
                console.log("Hello " + this.name);
            };
        };

        base.prototype.showName = function () {
            console.log(this.name);
        };

        function sub() {
            this.name = "sub";
        };

        util.inherits(sub, base);

        var objBase = new base();
        objBase.showName();
        objBase.sayHello();
        console.log(objBase);

        var objSub = new sub();
        objSub.showName();
        //objSub.sayHello();
        console.log(objSub);
    }

    this.inspect = function () {
        var util = require("util");

        function person() {
            this.name = "byvoid";

            this.toString = function () {
                return this.name;
            };
        };

        var obj = new person();

        console.log(util.inspect(obj));
        console.log(util.inspect(obj, true));
    };

    this.emit = function () {
        var events = require("events");
        var emitter = new events.EventEmitter();

        emitter.on("someEvent", function (arg1, arg2) {
            console.log("listener1", arg1, arg2);
        });

        emitter.on("someEvent", function (arg1, arg2) {
            console.log("listener2", arg1, arg2);
        });

        emitter.emit("someEvent", "byvoid", 1991);
    };

    this.error = function () {
        var events = require("events");
        var emitter = new events.EventEmitter();
        emitter.emit("error");
    };

    this.readFile = function () {
        var fs = require("fs");
        fs.readFile("context.txt", function (error, data) {
            if (error) {
                console.log(error);
            } else {
                console.log(data);
            }
        });
    };

    this.readFile2 = function () {
        var fs = require("fs");
        fs.readFile("context.txt", "utf-8", function (error, data) {
            if (error) {
                console.log(error);
            } else {
                console.log(data);
            }
        });
    };

    this.read = function () {
        var fs = require("fs");
        fs.open("context.txt", "r", function (error, fd) {
            if (error) {
                console.log(error);
                return;
            }

            var buf = new Buffer(8);
            fs.read(fd, buf, 0, 8, null, function (error, bytesRead, buffer) {
                if (error) {
                    console.log(error);
                    return;
                }
                console.log("bytesRead " + bytesRead);
                console.log(buffer);
            });

            fs.close(fd);
        });
    };

    this.server = function () {
        var http = require("http");

        http.createServer(function (req, res) {
            res.writeHead(200, {"Content-Type": "text/html"});
            res.write("<h1>Node.js</h1>");
            res.end("<p>Hello world</p>");
        }).listen(3000);

        console.log("HTTP server is listening at port 3000.");
    };

    this.server2 = function () {
        var http = require("http");

        var server = new http.Server();
        server.on("request", function (req, res) {
            res.writeHead(200, {"Content-Type": "text/html"});
            res.write("<h1>Node.js</h1>");
            res.end("<p>Hello world</p>");
        });
        server.listen(3000);

        console.log("HTTP server is listening at port 3000.");
    };

    this.get = function () {
        var http = require("http");
        var url = require("url");
        var util = require("util");

        http.createServer(function (req, res) {
            res.writeHead(200, {"Content-Type": "text/html"});
            res.end(util.inspect(url.parse(req.url, true)));
        }).listen(3000);
    };

    this.post = function () {
        var http = require("http");
        var querystring = require("querystring");
        var util = require("util");

        http.createServer(function (req, res) {
            var post;
            req.on("data", function (chunk) {
                post += chunk;
            });

            req.on("end", function () {
                post = querystring.parse(post);
                res.end(util.inspect(post));
            });
        }).listen(3000);
    };

    this.http = function () {
        var http = require("http");
        var querystring = require("querystring");

        var contents = querystring.stringify({
            name: "byvoid",
            email: "byvoid@byvoid.com",
            address: "Zijing 2#, Tsinghua University"
        });

        var options = {
            host: "www.byvoid.com",
            path: "/applicaiotn/node/post.php",
            method: "POST",
            headers: {
                "Content-Type": "application/x-www-urlencoded",
                "Content-Length": contents.length
            }
        };

        var req = http.request(options, function (res) {
            res.setEncoding("utf-8");
            res.on("data", function (data) {
                console.log(data);
            });
        });

        req.write(contents);
        req.end;
    };

    this.http2 = function () {
        var http = require("http");
        http.get({host: "www.byvoid.com"}, function (res) {
            res.setEncoding("utf-8");
            res.on("data", function (data) {
                console.log(data);
            });
        });
    };

    this.clientRequest = function () {
        var http = require("http");
        var req = http.get({host: "www.byvoid.com"});
        req.on("response", function (res) {
            res.setEncoding("utf-8");
            res.on("data", function (data) {
                console.log(data);
            });
        });
    };
}

